package com.yy.job.core.config.validator;

import org.apache.commons.lang3.StringUtils;

import com.yy.job.common.enums.ErrorCodeEnum;
import com.yy.job.core.config.xml.JobStepXmlConfig;
import com.yy.job.utils.DataValidator;
import com.yy.job.utils.exception.YYJobRuntimeException;

/**
 * 校验器
 * 
 * @author eleven.li
 *
 */
public class JobStepXmlConfigValidator {

	public static void validate(String moduleCode, String jobCode, JobStepXmlConfig jobStepXmlConfig) {
		if (jobStepXmlConfig == null) {
			throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_STEP_ERROR, moduleCode, jobCode, "no jobStepConfig");
		}

		String stepCode = jobStepXmlConfig.getStepCode();
		if (StringUtils.isBlank(stepCode)) {
			throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_STEP_CONFIG, moduleCode, jobCode, "", "stepCode is empty");
		}
		if (StringUtils.isBlank(jobStepXmlConfig.getStepName())) {
			throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_STEP_CONFIG, moduleCode, jobCode, stepCode,
					"stepName is empty");
		}
		if (StringUtils.isBlank(jobStepXmlConfig.getStepProcessBean())) {
			throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_STEP_CONFIG, moduleCode, jobCode, stepCode,
					"stepProcessBean is empty");
		}
		if(StringUtils.isBlank(jobStepXmlConfig.getStepOrder())) {
			throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_STEP_CONFIG, moduleCode, jobCode, stepCode,
					"stepOrder is empty");
		}
		if (StringUtils.isNotBlank(jobStepXmlConfig.getStepOrder())
				&& !DataValidator.isPosInteger(jobStepXmlConfig.getStepOrder())) {
			throw new YYJobRuntimeException(ErrorCodeEnum.CHECK_STEP_CONFIG, moduleCode, jobCode, stepCode,
					"stepOrder is not positive integer");
		}
	}

}
